﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Web7.Container.Models;
using Web7.Container.Services;
using Web7.Core.Security;
using Web7.Mvc.ModelBinders;

namespace Web7.Container.Controllers
{
    public class PermissionRecordController : AdminController
    {
        private IPermissionRecordService _PermissionRecordService;

        [Dependency]
        public IPermission PermissionProviderService { get; set; }

        [Dependency]
        public IAuthenticationService AuthenticationService { get; set; }

        [Dependency]
        public IUserService UserService { get; set; }

        [Dependency]
        public IRoleService RoleService { get; set; }

        [Dependency]
        public IAssistUserService<Guid> AssistUserService { get; set; }

        [Dependency]
        public IAssistRoleService<Guid> AssistRoleService { get; set; }

        [Dependency]
        public IPermissionRecordService PermissionRecordService { get; set; }

        public PermissionRecordController(IPermissionRecordService permissionRecordService)
        {
            _PermissionRecordService = permissionRecordService;
        }

        public ActionResult Index(PermissionOwnerType ownerType, Guid ID)
        {
            IList<Permission> permissions = PermissionProviderService.GetAllPermissions();
            OwnerPermissionRecordContent model = new OwnerPermissionRecordContent();
            switch (ownerType)
            {
                case PermissionOwnerType.User:
                    {
                        OwnerPermissionRecord<User> user = new OwnerPermissionRecord<User>();
                        user.PermissionRecords = permissions;
                        user.Owner = UserService.Get(ID);
                        user.OwnerPermissionRecords = AssistUserService.GetPermissionsForUser(ID).ToList();
                        model.Content = user;
                    }
                    break;
                case PermissionOwnerType.Role:
                    {
                        OwnerPermissionRecord<Role> role = new OwnerPermissionRecord<Role>();
                        role.PermissionRecords = permissions;
                        role.Owner = RoleService.Get(ID);
                        role.OwnerPermissionRecords = AssistRoleService.GetPermissionsForRole(ID).ToList();
                        model.Content = role;
                    }
                    break;
            }
            model.OwnerType = ownerType;
            model.ID = ID;

            return View(model);
        }

        [HttpPost, ActionName("Index")]
        public ActionResult Edit([ModelBinder(typeof(GuidListModelBinder))]List<Guid> PermissionIds, PermissionOwnerType ownerType, Guid ID)
        {
            List<PermissionRecord> permissions = new List<PermissionRecord>();

            switch (ownerType)
            {
                case PermissionOwnerType.User:
                    permissions = AssistUserService.GetPermissionsForUser(ID).ToList();
                    break;
                case PermissionOwnerType.Role:
                    permissions = AssistRoleService.GetPermissionsForRole(ID).ToList();
                    break;
            }

            foreach (var id in PermissionIds)
            {
                if (!permissions.Exists(p => p.ActionId.Equals(id)))
                {
                    PermissionRecordService.Create(new PermissionRecord() { Id = SequentialGuid.NewGuid(), OwnerId = ID, ActionId = id, OwnerType = ownerType });
                }
            }

            foreach (var permissionRecord in permissions)
            {
                if (!PermissionIds.Exists(id => id.Equals(permissionRecord.ActionId)))
                {
                    PermissionRecordService.Delete(p => p.ActionId == permissionRecord.ActionId && p.OwnerId == ID);
                }
            }

            return RedirectToAction("Index");
        }
    }
}
